package cn.jiyun.kafka.listener;

import cn.jiyun.kafka.pojo.User;
import com.alibaba.fastjson.JSON;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class HelloListener {
//    @KafkaListener(topics = {"kafka-hello"})

    @KafkaListener(topics = {"kafka-hello-obj"})
    public void receiverMessage(ConsumerRecord<?,?>record){
//        Optional<? extends ConsumerRecord<?,?>> optional = Optional.ofNullable(record);
//        if (optional.isPresent()) {
//            Object value = record.value();
//            System.out.println("HelloListener接收到消息="+value);
//        }

        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if (optional.isPresent()) {
            Object value = record.value();
            User user = JSON.parseObject((String) value, User.class);
            System.out.println("HelloObjectListener接收到消息="+user);
        }


    }
}
